﻿using Business;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace BVVD_Website.ql_ketqua_hoctap
{
    public partial class frmGiayNhanxet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BVVDDataContext db = new BVVDDataContext();
                var listLOPHOC = (from lh in db.LST_LOPHOCs
                                  where lh.TRANGTHAI.Equals("1")
                                  select lh).ToList();

                ddlLOPHOC.DataSource = listLOPHOC;
                ddlLOPHOC.DataTextField = "TENLH";
                ddlLOPHOC.DataValueField = "MALH";
                ddlLOPHOC.DataBind();
                grvGiayNhanxet.DataSource = getData();
                grvGiayNhanxet.DataBind();

            }
        }

        protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            BVVDDataContext db = new BVVDDataContext();
            var list = (from l in db.LST_HOCVIENs
                        where l.TRANGTHAI.Equals("1")
                        select new
                        {
                            NAME = l.HODEM + " " + l.TENHV + " " + l.MAHV + " " + l.NGAYSINH,
                            MAHV = l.MAHV,
                            FULLNAME = l.HODEM + " " + l.TENHV,
                            NGAYSINH = l.NGAYSINH
                        }).ToList();
            e.Result = list;
        }

        protected void ddlLOPHOC_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
        {
            grvGiayNhanxet.DataSource = getData();
            grvGiayNhanxet.DataBind();
        }

        private List<F_GIAYNX> getData()
        {

            BVVDDataStore<F_GIAYNX> db = new BVVDDataStore<F_GIAYNX>();
            Hashtable condition = new Hashtable();
            condition.Add("MALH", ddlLOPHOC.SelectedValue);
            return db.getFromDB(ddlLOPHOC.SelectedIndex >= 0 ? condition : new Hashtable());

        }

        protected void RadSearchBox1_Search(object sender, Telerik.Web.UI.SearchBoxEventArgs e)
        {
            RadSearchBox chkbox = (RadSearchBox)sender;
            GridEditFormItem editItem = (GridEditFormItem)chkbox.NamingContainer;
            RadTextBox txtMAHV = (RadTextBox)editItem.FindControl("txtMAHV");
            //RadTextBox txtMALH = (RadTextBox)editItem.FindControl("txtMAHV");
            if (e.DataItem != null)
            {
                txtMAHV.Text = ((Dictionary<string, object>)e.DataItem)["MAHV"].ToString();
            }
        }

        protected void grvGiayNhanxet_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            grvGiayNhanxet.DataSource = getData();
        }

        protected void grvGiayNhanxet_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editFormItem = e.Item as GridEditFormItem;
                GridDataItem parentItem = editFormItem.ParentItem;

                RadioButtonList rblTRANGTHAI = editFormItem.FindControl("rblTRANGTHAI") as RadioButtonList;
                RadTextBox txtMALH = editFormItem.FindControl("txtMALH") as RadTextBox;

                if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
                {
                    txtMALH.Text = ddlLOPHOC.SelectedValue;
                }
                else
                {

                    rblTRANGTHAI.SelectedValue = editFormItem["TRANGTHAI"].Text;

                }

            }
        }

        protected void grvGiayNhanxet_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;

            F_GIAYNX gnx = new F_GIAYNX
            {
                SOGNX = (insertedItem.FindControl("txtSOGNX") as RadTextBox).Text,
                MALH = (insertedItem.FindControl("txtMALH") as RadTextBox).Text,
                MAHV = (insertedItem.FindControl("txtMAHV") as RadTextBox).Text,
                GHICHU = (insertedItem.FindControl("txtGHICHU") as RadTextBox).Text,
                NGAYNHAN = (insertedItem.FindControl("rdpNGAYNHAN") as RadDatePicker).SelectedDate,
                TRANGTHAI = (insertedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue

            };
            BVVDDataContext db = new BVVDDataContext();
            db.F_GIAYNXes.InsertOnSubmit(gnx);
            try
            {
                db.SubmitChanges();
               // rnAlert.Text = "Thêm mới giấy nhận xét cho " + gnx.LST_HOCVIEN.HODEM + " " + gnx.LST_HOCVIEN.TENHV + " ở lớp " + gnx.LST_LOPHOC.TENLH + " thành công ";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra \n" + ex.Message;

            }


            rnAlert.Show();
        }

        protected void grvGiayNhanxet_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string recid = item.OwnerTableView.DataKeyValues[item.ItemIndex]["RECID"].ToString();
            BVVDDataContext db = new BVVDDataContext();
            F_GIAYNX gnx = (from dm in db.F_GIAYNXes
                            where dm.RECID.ToString().Equals(recid)
                            select dm).SingleOrDefault();
            db.F_GIAYNXes.DeleteOnSubmit(gnx);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Xóa thành công giấy nhận xét số  " + gnx.SOGNX;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n " + ex.Message;
            }

            rnAlert.Show();
        }
    }
}